//
//  HYCTourPalPushtroller.m
//  FamilyTravel
//
//  Created by dllo on 15/10/29.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCTourPalPushtroller.h"
#import "HYCHeadDocument.h"
#import "HYCtravelSaveTool.h"

#define SWIDTH ([UIScreen mainScreen].bounds.size.width)
#define SHEIGH ([UIScreen mainScreen].bounds.size.height)

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface HYCTourPalPushtroller ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, retain) NSMutableDictionary *dic;
@property(nonatomic, retain) NSMutableArray *zArr;
@property(nonatomic, retain) MBProgressHUD *hud;

@end

@implementation HYCTourPalPushtroller
- (void)dealloc{
    [_arr release];
    [_tableView release];
    [_dic release];
    [_zArr release];
    [_hud release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    if ([HYCtravelSaveTool isModelInPlist:self.model]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    } else {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"已收藏" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    }
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"加载中...";
    
    
    [self createData];
    
}
- (void)click:(UIBarButtonItem *)button {
    
    if ([button.title isEqualToString:@"已收藏"]) {
        [HYCtravelSaveTool cancelModelInPlist:self.model];
        button.title = @"收藏";
        
        UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"😄" message:@"取消收藏成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
        
        
    } else {
        
        UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"😄" message:@"收藏成功了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
        [HYCtravelSaveTool saveModelToPlist:self.model];
        button.title = @"已收藏";
        
    }
}

- (void)createData {
    NSString *strUrl = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v2/travelog/%@?app_version=2.4.2&device_type=1", self.str];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.dic = [NSMutableDictionary dictionary];
        self.dic = responseObject;
        self.arr = [HYCTourPalpushModel baseModelByArr:self.dic[@"log"][@"posts"]];
        [self.tableView reloadData];
        [self.hud removeFromSuperview];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HYCTourPalSecendPushTroller *tPSPVC = [[HYCTourPalSecendPushTroller alloc]init];
    
    tPSPVC.str = [self.arr[indexPath.row] mdescription];
    
    tPSPVC.pStr = [self.arr[indexPath.row] pictures][0][@"picture"];
    
    NSLog(@"%@", tPSPVC.pStr);
    
    [self.navigationController pushViewController:tPSPVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuse = @"reuse";
    
    HYCTourPalPushCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[HYCTourPalPushCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    
    cell.timeLabel.text = self.dic[@"log"][@"start_date"];
    
    cell.label.text = [self.arr[indexPath.row] mdescription];
    cell.label.contentMode = UIViewContentModeTop;
    cell.label.numberOfLines = 0;
    
    [cell.tuImageView sd_setImageWithURL:[NSURL URLWithString:[self.arr[indexPath.row] pictures][0][@"picture"]]placeholderImage:[UIImage imageNamed:@"zw.png"]];
    
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.alpha = 1;
    [UIView commitAnimations];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // label的自适应和字体大小还有关系 妈妈的
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12], NSFontAttributeName, nil];
    
    CGRect rect = [[self.arr[indexPath.row] mdescription] boundingRectWithSize:CGSizeMake(SWIDTH, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height + 227;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
